//
//  StrategyCollectionViewCell.m
//  礼物说
//
//  Created by dllo on 16/4/26.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "StrategyCollectionViewCell.h"

@interface StrategyCollectionViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray *zhongZhuanArr;
@property (nonatomic, retain) NSMutableArray *zhuanTiZhongZhuanArr;

@end

@implementation StrategyCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollection];
    }
    return self;
}
- (void)dealloc{
    [_zhuanTiZhongZhuanArr release];
    [_zhuanTiArr release];
    [_zhongZhuanArr release];
    [_strategyArr release];
    [_collectionView release];
    [super dealloc];
}

- (void)setZhuanTiArr:(NSMutableArray *)zhuanTiArr {
    if (_zhuanTiArr != zhuanTiArr) {
        [_zhuanTiArr release];
        _zhuanTiArr = [zhuanTiArr retain];
        self.zhuanTiZhongZhuanArr = [NSMutableArray arrayWithArray:_zhuanTiArr];
        [_collectionView reloadData];
    }
}
- (void)setStrategyArr:(NSMutableArray *)strategyArr {
    if (_strategyArr != strategyArr) {
        [_strategyArr release];
        _strategyArr = [strategyArr retain];
        self.zhongZhuanArr = [NSMutableArray arrayWithArray:_strategyArr];
        [_collectionView reloadData];
    }
}
#pragma mark - 创建CollectionView
- (void)createCollection{
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, kWIDTHCELL, kHEIGHTCELL) collectionViewLayout:layout];
    _collectionView.pagingEnabled = NO;
//    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_collectionView];
    [_collectionView release];
    [layout release];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[HeadCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headCell"];
    
    [_collectionView registerClass:[GongLvCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [_collectionView registerClass:[ZhuanTiCollectionViewCell class] forCellWithReuseIdentifier:@"ZhuanTiColl"];
}

/**cell的个数*/
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        StrategyBeforeModel * model = _zhongZhuanArr[section - 1];
        return model.dataArr.count;
    }
}


- (UICollectionViewCell * )collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        ZhuanTiCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZhuanTiColl" forIndexPath:indexPath];
        cell.zhuanTiArr = _zhuanTiZhongZhuanArr;
        [cell.collecitonView reloadData];
        return cell;
        
    }else{
        
        GongLvCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        StrategyBeforeModel *model = _zhongZhuanArr[indexPath.section - 1];
        
        cell.model = model.dataArr[indexPath.row];
        return cell;
    }
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _zhongZhuanArr.count + 1;
    
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        return CGSizeMake(kWIDTHCELL, kHEIGHTS(70));
    }else{
        
        return CGSizeMake(kWIDTHCELL / 4, kWIDTHCELL / 4 + kWIDTHS(20));
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(kWIDTHCELL, kWIDTHS(30));
}

- (UICollectionReusableView * )collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        HeadCollectionReusableView * header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headCell" forIndexPath:indexPath];
        
        if (indexPath.section == 0) {
            
            header.str = @"专题";
            
            return header;
            
        }else{
            
            header.model = _zhongZhuanArr[indexPath.section - 1];
            
            return header;
        }
    }
    return nil;
}

/**点击方法 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ClassifyGongLvViewController * gongLvVc = [[ClassifyGongLvViewController alloc] init];
    StrategyBeforeModel * model = _zhongZhuanArr[indexPath.section - 1];
    StrategyModel * strateModel = model.dataArr[indexPath.row];
    gongLvVc.num = strateModel.number;
    gongLvVc.Judge = @"gongLv";
    gongLvVc.titles = strateModel.name;
    UIViewController * ViewController = [self viewController];
    
    [ViewController.navigationController pushViewController:gongLvVc animated:YES];
    [gongLvVc release];

}

- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}



@end
